<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-2.1.1.min.js"></script>
    <script src="/js/my97/WdatePicker.js"></script>
    <link rel="stylesheet" href="/js/uploadify/uploadify.css" />
    <script src="/js/uploadify/jquery.uploadify.min.js"></script>
    <link rel="stylesheet" href="/js/kindeditor-4.1.10/themes/default/default.css" />
    <script src="/js/kindeditor-4.1.10/kindeditor-all.js"></script>
    <link rel="stylesheet"href="/js/easyui1.5/themes/default/easyui.css"type="text/css"></link>
    <link rel="stylesheet"href="/js/easyui1.5/themes/icon.css"type="text/css"></link>
    <script type="text/javascript" src="/js/easyui1.5/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui1.5/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<a id="btn_add" onclick="addData()" href="javascript:void(0)"class="easyui-linkbutton" data-options="iconCls:'icon-remove'">增加</a>

<table id="dg" >

</table>
<!-- 弹框增加start -->
<div id="dialog_add" class="easyui-dialog" title="My Dialog"
     style="width:800px;height:400px;"
     data-options="iconCls:'icon-save',resizable:true,modal:true,
				buttons:[{
					text:'保存',
					iconCls:'icon-no',
					handler:function(){
						submitData()//调用另一个方法
					}
				},{
					text:'关闭',
					iconCls:'icon-more',
					handler:function(){
						closedSubmitData()
						 $('#dialog_add').dialog('close');
					}
				}]">
    <form id="add_form">
        <input type="hidden" name="id">
        角色:<input name="rname" class="easyui-textbox" data-options="iconCls:'icon-man'" style="width:200px"><br>
    </form>
</div>
<!-- 弹框增加end -->

</body>
<script type="text/javascript" th:inline="none">
    $(function(){
        getHaa();

        $('#dialog_add').dialog('close');
        normal2ajax();

    })

    function  getHaa(){
        $("#dg").datagrid({
            url:'/view/getHaa',
            pagination:true,
            toolbar:"#gongneng",
            pageList:[5,8,10],
            pageSize:8,
            checkOnSelect:false,
            columns:[[
                {field:'xxx',title:'xx',width:100,checkbox:true},
                {field:'id',title:'ID',width:100},
                {field:'rname',title:'角色',width:100},
                {field:'xsxs',title:'操作',width:300,
                    formatter:function(value,row,index){
                        var content = " <a href='JavaScript:delRole("+row.id+")' >删除</a> ";
                        content += " <a href='JavaScript:updaRole("+row.id+")' >修改</a> ";
                        content+=" | <a href='/role/toPower?url=rolePer&id="+row.id+"'>权限切换</a>";
                        return content;
                    }
                }
            ]]

        })
    }

    function delRole(id){
        $.messager.confirm('确认','您确认想要删除这个角色记录吗？',function(r){
            if (r){//点击确认
                deleteItems(id);
            }
        });
    }
    /*function setEvent(){
        $("#btn_delete").linkbutton({
            onClick:function(){
                var rows=$("#dg").datagrid("getChecked");
                var ids="";
                $.each(rows,function(index,obj){
                    ids+=ids==''?obj.id:","+obj.id;
                });
                deleteItems(ids);
            }
        });
    }*/
    function deleteItems(ids){
        $.ajax({
            url:"/view/delRole",
            type:"POST",
            data:{id:ids},
            success:function(pageUtil){
                /* deleteItems start */
                $.messager.show({
                    title:'删除',
                    msg:'删除成功',
                    timeout:2000,
                    showType:'slide'
                });
                getHaa();
            },
            error:function(pageUtil){
                if(pageUtil==0){
                    alert("代码错误");
                }else{
                    alert("删除失败");
                }
            }
        });
    }
    /* 修改start */
    function updaRole(id){
        $.ajax({
            url:"/view/updaRole",
            type:"POST",
            data:{id:id},
            success:function(result){
                $("#dialog_add").dialog("open");
                $('#add_form').form('load',result);
            },
            error:function(){
                alert("失败了");
            }
        });
    }
    /* 修改end */
    /* 提交================================================> start*/
    function normal2ajax(){
        $('#add_form').form({
            url:'/view/addRole',
            onSubmit: function(){
            },
            success:function(data){
                if(data==1){
                    alert("不能添加重复的编号");
                }else{
                    closedSubmitData();
                    $("#dialog_add").dialog("close");
                    $('#dg').datagrid('reload');
                }
            }
        });
    }
    function submitData(){//进行提交表单
        $("#add_form").form('submit')
    }
    function addData(){
        $("#dialog_add").dialog("open");
        closedSubmitData();
    }
    function closedSubmitData(){
        $('#dialog_add').form("clear");
    }

</script>
</html>